package com.aplink.generic.widgets;

import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.menu.MenuItemSpec;
import com.aplink.generic.pojo.controlspec.menu.PopupMenuSpec;
import com.aplink.generic.util.Language;

public class ApLinkPopupMenu extends ApLinkBaseView {
	private final View mAnchorView;
	private final View.OnClickListener mClickListener = new OnClickListener() {

		@Override
		public void onClick(final View v) {
			ApLinkPopupMenu.this.mPopupMenu.show();
		}
	};
	private GenericObject mGenericObject;
	private PopupMenu mPopupMenu;

	private PopupMenuSpec mPopupMenuSpec;

	private final OnMenuItemClickListener onPopupMenuItemClick = new OnMenuItemClickListener() {

		@Override
		public boolean onMenuItemClick(final MenuItem item) {
			if (!ApLinkPopupMenu.this.mPopupMenuSpec.getPopupMenuItems()
			        .isEmpty()) {
				final MenuItemSpec popupMenuItem = ApLinkPopupMenu.this.mPopupMenuSpec
				        .getPopupMenuItems().get(item.getItemId());
				popupMenuItem
				        .setGenericObject(ApLinkPopupMenu.this.mGenericObject);
				try {
					ApLinkPopupMenu.this.getGenericFragment()
					        .onHandlePopupMenuItem(popupMenuItem);
				} catch (final NameNotFoundException e) {
					e.printStackTrace();
				} catch (final ClassNotFoundException e) {
					e.printStackTrace();
				}
			}
			return false;
		}
	};

	public ApLinkPopupMenu(final GenericFragment genericFragment,
	        final ControlSpec controlSpec, final View view, Bundle bundle) {
		super(genericFragment, controlSpec, view, bundle);
		this.mAnchorView = view;
		this.mPopupMenuSpec = (PopupMenuSpec) controlSpec;
	}

	@Override
	public void clear() {

	}

	@Override
	public GenericAttribute getAttribute() {
		return null;
	}

	public GenericObject getGenericObject() {
		return this.mGenericObject;
	}

	@Override
	public GenericDictionary<String, GenericAttribute> getListAttributes() {
		return null;
	}

	public PopupMenu getPopupMenu() {
		return this.mPopupMenu;
	}

	public PopupMenuSpec getPopupMenuSpec() {
		return this.mPopupMenuSpec;
	}

	@Override
	public View getView() {
		return null;
	}

	@Override
	protected void initViewProperties() {
		this.mPopupMenu = new PopupMenu(getActivity(), this.mAnchorView);
		for (int i = 0; i < this.mPopupMenuSpec.getPopupMenuItems().size(); i++) {
			String title = Language.getInstance().get(
			        this.mPopupMenuSpec.getPopupMenuItems().get(i).getTitle());
			this.mPopupMenu.getMenu().add(Menu.NONE, i, Menu.NONE, title);
		}
	}

	@Override
	public void languageChanged() {
		for (int i = 0; i < mPopupMenu.getMenu().size(); i++) {
			mPopupMenu
			        .getMenu()
			        .getItem(i)
			        .setTitle(
			                Language.getInstance().get(
			                        mPopupMenu.getMenu().getItem(i).toString()));
		}
	}

	@Override
	protected void loadBindingObject() {

	}

	@Override
	protected void loadDefaultObject() {

	}

	@Override
	protected void setEventListener() {
		this.mPopupMenu.setOnMenuItemClickListener(this.onPopupMenuItemClick);
		this.mAnchorView.setOnClickListener(this.mClickListener);
	}

	public void setGenericObject(final GenericObject genericObject) {
		this.mGenericObject = genericObject;
	}

	@Override
	public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {
		throw new UnsupportedOperationException(this.getClass().getSimpleName()
		        + " is not support this method");
	}

	@Override
	public void setOnClickListener(final OnClickListener onClickListener) {
		throw new UnsupportedOperationException(this.getClass().getSimpleName()
		        + " is not support this method");
	}

	public void setPopupMenu(final PopupMenu popupMenu) {
		this.mPopupMenu = popupMenu;
	}

	public void setPopupMenuSpec(final PopupMenuSpec popupMenuSpec) {
		this.mPopupMenuSpec = popupMenuSpec;
	}
}
